package com.wn.cleandemo.utils.storage;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 磁盘挂载管理工具
 */
public class StorageUtil {
    private static StorageHolder holder;
    private static final Object sLock = new Object();

    private static StorageHolder GetHolder() {
        StorageHolder result = null;

        synchronized (sLock) {
            if (null == holder) {
                holder = new StorageHolder();
            }
            result = holder;
        }
        return result;
    }

    /**
     * 获取所有挂载路径
     */
    public static List<String> getAllMountedStoragePath() {
        List<String> temp = GetHolder().GetAllMountedStorage();
        if (null != temp) {
            return temp;
        }
        List<String> arrayList = new ArrayList<>();
        arrayList.add(Environment.getExternalStorageDirectory().getPath());
        return arrayList;
    }

    /**
     * 是否有存储权限
     */
    public static boolean isGrantedStoragePermission(Context context) {
        if (Build.VERSION.SDK_INT >= 30) {
            return Environment.isExternalStorageManager();
        } else if (Build.VERSION.SDK_INT >= 23) {
            return context.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                    && context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
        }
        return true;
    }

    /**
     * 获取磁盘大小情况
     *
     * @param context
     * @return long数组 0=AvailableBytes 1=TotalBytes
     */
    public static long[] getStorageSize(Context context) {
        List<String> sdcard = getAllMountedStoragePath();
        long a = 0;
        long b = 0;
        if (null != sdcard && sdcard.size() > 0) {
            for (String s : sdcard) {
                if (!TextUtils.isEmpty(s)) {
                    long[] size = getStorageOnPath(s);
                    a += size[0];
                    b += size[1];
                }
            }
        }
        if (b <= 0 && context != null) {
            try {
                long[] size = getStorageOnPath(context.getApplicationContext().getCacheDir().getAbsolutePath());
                return size;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new long[]{a, b};
    }

    private static long[] getStorageOnPath(String path) {
        long AvailableBytes = 0;
        long TotalBytes = 0;
        try {
            if (!TextUtils.isEmpty(path)) {
                StatFs statFs = new StatFs(path);

                if (Build.VERSION.SDK_INT >= 18) {
                    AvailableBytes = statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong();
                    TotalBytes = statFs.getBlockCountLong() * statFs.getBlockSizeLong();
                } else {
                    AvailableBytes = statFs.getAvailableBlocks() * statFs.getBlockSize();
                    TotalBytes = statFs.getBlockCount() * statFs.getBlockSize();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new long[]{AvailableBytes, TotalBytes};
    }


}
